package com.example.cyh.bczapp.Code.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cyh.bczapp.Code.BLL.WordBLL;
import com.example.cyh.bczapp.Code.Model.Tran;
import com.example.cyh.bczapp.Code.Model.WordBase;
import com.example.cyh.bczapp.Code.Model.WordExpand;
import com.example.cyh.bczapp.R;

/**
 * Created by cyh on 2017/5/8.
 */

public class FindWordFragment extends Fragment {
    private View AllView;
    private Handler  handler=new Handler();
    private Tran  tran;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        AllView = inflater.inflate(R.layout.fragment_review, container, false);
        return AllView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        AllView.findViewById(R.id.find).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String word = ((EditText) AllView.findViewById(R.id.ed_word)).getText().toString();
                            WordBLL wb = new WordBLL();
                            tran = wb.FindWord(word);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        if (tran == null) {
                                            ((TextView) AllView.findViewById(R.id.words)).setText("抱歉 没有找到这个单词!");
                                            return;
                                        } else {
                                            //设置汉语意义
                                            String Chinese = "";
                                            for (String inf : tran.basic.explains
                                                    ) {
                                                Chinese += inf;
                                            }

                                            ((TextView) AllView.findViewById((R.id.word_china)))
                                                    .setText(Chinese);//word_soundmark
                                            //设置音标
                                            ((TextView) AllView.findViewById((R.id.word_soundmark)))
                                                    .setText(tran.basic.phonetic);
                                            //设置例句
                                            String words = "";
                                            for (WordExpand wordexpand : tran.web
                                                    ) {
                                                String value = "";
                                                for (String va : wordexpand.value
                                                        ) {
                                                    value += va + "|";
                                                }
                                                words += "词组:" + wordexpand.key + "-------->>" + value + "\n\r";
                                            }
                                            ((TextView) AllView.findViewById((R.id.words)))
                                                    .setText(words);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Toast.makeText(getActivity(),"没有查找到相关信息",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                        catch (Exception  ex)
                        {
                            Toast.makeText(getActivity(),"没有查找到相关信息",Toast.LENGTH_SHORT).show();
                        }
                    }
                }).start();
            }
        });
    }

}
